package com.cm386.final_project_timezonealert.model;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.widget.TimePicker;

import com.cm386.final_project_timezonealert.EventFieldActivity;

public class EventTimePickerDialogFragment extends DialogFragment{
	public static final String INIT_HOUR_KEY 	   = "INIT_HOUR";
	public static final String INIT_MINUTES_KEY    = "INIT_MINUTES";
	private static EventTimePickerDialogFragment timePicerDialogFragment;
	

	public static EventTimePickerDialogFragment getInstance(int initHour, int initMinutes){
		if (timePicerDialogFragment == null)
			timePicerDialogFragment = new EventTimePickerDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(INIT_HOUR_KEY, initHour);
		bundle.putInt(INIT_MINUTES_KEY, initMinutes);
		timePicerDialogFragment.setArguments(bundle);
		return timePicerDialogFragment;
	}
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		int initHour 	= getArguments().getInt(INIT_HOUR_KEY);
		int initMinutes = getArguments().getInt(INIT_MINUTES_KEY);
		// Use the Builder class for convenient dialog construction
		MyTimePickerDialog dialog = new MyTimePickerDialog(getActivity(),new OnTimeSetListener() {  
			int initHour;
			int initMinutes;
			
			private OnTimeSetListener init(int initHour, int initMinutes) {
				// TODO Auto-generated method stub
				this.initHour 	 = initHour;
				this.initMinutes = initMinutes;
				return this;
			}
			
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
				// TODO Auto-generated method stub  
				if (hourOfDay != initHour 	||
					minute	  != initMinutes){
					EventFieldActivity callingActivity = (EventFieldActivity) getActivity();
					callingActivity.timePickerSetTimeCallBack(hourOfDay, minute);
				}
				//System.out.println("hour-->"+hourOfDay+"  minute-->"+ minute);  
			}
		}.init(initHour, initMinutes), initHour, initMinutes, true);
		// TODO Auto-generated method stub
		return dialog;
	}
}